import { descNumber } from './sysFun';
/**
 * 获取表格百分比
 * @param tableData
 * @param sortName
 * @param sort
 * @returns {*}
 */
export const getRate = (tableData, sortName, sort = false) => {
  if (tableData && tableData.length && sortName) {
    if (sortName instanceof Array) {
      if (sortName.length) {
        let max = 0;
        sortName.map(item => {
          const temp = [...tableData].sort(descNumber(item));
          if (max < temp[0][item]) max = temp[0][item];
        });
        sortName.map(item => {
          tableData.map(record => {
            if (Number(max) === 0) {
              record[`${item}Rate`] = 0.1;
            } else {
              record[`${item}Rate`] = (Number(record[item] || 0) / Number(max)) * 100;
            }
          });
        });
      }
    } else {
      if (sort) tableData.sort(descNumber(sortName));
      const temp = [...tableData].sort(descNumber(sortName));
      const max = temp[0][sortName];
      tableData.map(item => {
        if (Number(max) === 0) {
          item[`${sortName}Rate`] = 0.1;
        } else {
          item[`${sortName}Rate`] = (Number(item[sortName] || 0) / Number(max)) * 100;
        }
      });
    }
  }
  return tableData;
};
/**
 * 获取合并行数组
 * @param tableData
 * @param mergeKey
 * @returns {any[]}
 */
export const getRowSpanList = (tableData = [], mergeKey = '') => {
  const rowSpanList = new Array(tableData.length);
  // 保存上一个值
  let value = '';
  // 相同值出现的次数
  let count = 0;
  // 该值第一次出现的位置
  let startIndex = 0;
  if (tableData.length) {
    tableData.map((item, i) => {
      const val = item[mergeKey];
      if (i === 0) {
        value = val;
        count = 1;
        rowSpanList[0] = 1;
      } else if (val === value) {
        count++;
        rowSpanList[startIndex] = count;
        rowSpanList[i] = 0;
      } else {
        count = 1;
        value = val;
        startIndex = i;
        rowSpanList[i] = 1;
      }
    });
  }
  return rowSpanList;
};
/**
 * 获取合并行Render
 * @param value
 * @param index
 * @param tableData
 * @param mergeKey
 * @returns {{children: *, props: {}}}
 */
export const getRowSpanRender = (value, index, tableData = [], mergeKey = '') => {
  const obj = {
    children: value,
    props: {},
  };
  obj.props.rowSpan = getRowSpanList(tableData, mergeKey)[index];
  return obj;
};
/**
 * 计算表格scrollX宽度
 * @param columns
 * @param clientWidth
 * @returns {number}
 */
export const getTableWidth = (columns = [], clientWidth) => {
  const columnsWidth = getColumnsWidth(columns);
  return columnsWidth > clientWidth ? columnsWidth : 0;
};
export const getColumnsWidth = (columns = []) => {
  let width = 0;
  columns.map(item => {
    if (item.children && item.children.length) {
      width += getColumnsWidth(item.children);
    } else if (item.width) {
      width += item.width;
    } else {
      width += Math.ceil(200 * window.viewportScale);
    }
  });
  return width;
};
/**
 * 表格列宽度缩放
 * @param columns
 * @returns {Array}
 */
export const scaleColumnsWidth = (columns = []) => {
  const c = [];
  columns.map(item => c.push({ ...item }));
  c.map(item => {
    if (item.children && item.children.length) {
      item.children = scaleColumnsWidth(item.children);
    } else if (item.width) {
      item.width = Math.ceil(item.width * window.viewportScale);
    }
  });
  return c;
};
